package com.york.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

/**
 * @Description:
 * @Data: 2022/3/20 10:33
 * @Author: yangboxiong
 * @Version
 * @since JDK 1.8
 */
@Configuration       //表示当前类是一个配置类（相当于springmvc.xml配置文件），常用在无法用@Service、@Repository描述的Spring管理的类上，相当于通用的注解,默认对象名称；
public class CorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        CorsConfiguration corsConfiguration = new CorsConfiguration();

        //1.配置跨域
        corsConfiguration.addAllowedHeader("*");     //允许携带的头信息
        corsConfiguration.addAllowedMethod("*");     //允许任何方法（post、get等）
        corsConfiguration.addAllowedOrigin("*");     //允许任何域名使用
        corsConfiguration.setAllowCredentials(true); //是否允许携带cookie
        corsConfiguration.setMaxAge(3600L);          //最大响应时间
        source.registerCorsConfiguration("/**", corsConfiguration);     //处理所有请求的跨域配置
        return new CorsWebFilter(source);
    }
}
